package com.syaccp.utils;

import com.syaccp.model.OrderDetail;
import com.syaccp.model.OrderInfo;
import com.syaccp.model.TOrderDetail;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * 订单数据枚举类
 *
 * @author Lyh
 * @since 2023-07-10
 */
public enum OrderRelatedUtil {
    /**
     * 订单订单品质
     */
    SYS_ORDER_QUALITY("sys_order_quality"),
    /**
     * 订单附件等级
     */
    SYS_ATTACHMENT_TYPE("sys_attachment_type"),
    /**
     * 订单风格分类
     */
    SYS_ORDER_FGFL("product_style"),
    /**
     * 下拉框存入redis Key值
     */
    ORDER_DROPDOWN("order_dropdown"),
    /**
     * 没有图片则显示
     */
    DEFAULT_PIC("http://127.0.0.1/images/404PhotoNotFound.png"),
    /**
     * 拼接图片地址前缀
     */
    PICTURE__PATH_PREFIX("https://api.hntututu.com/fullView/"),
    /**
     * 拼接图片地址连接
     */
    PICTURE_CONCAT("/panos/"),
    /**
     * 拼接图片地址后缀
     */
    PICTURE_PATH_SUFFIX(".tiles/thumb.jpg"),
    /**
     * 订单制作小组
     */
    SYS_ORDER_MAKE_TEAM("sys_order_make_team"),
    /**
     * 订单制作公司
     */
    SYS_ORDER_COMPANY("sys_order_company"),
    /**
     * 订单图片上传路径
     */
    SYS_UPLOAD_FILE_PATH(System.getProperty("user.dir").replace("/", "\\") + "\\target\\classes\\static\\uploadFiles\\");


    private final String message;

    OrderRelatedUtil(String msg) {
        this.message = msg;
    }

    public String getMessage() {
        return message;
    }


    /**
     * 拼接图片路径方法
     *
     * @param picturePath 图片名称
     */
    public static String setRealPicPath(String picturePath) {
        String PicturePath = "";
        if (picturePath.contains("_f.jpg")) {
            String keyword = picturePath.replace("_f.jpg", "");
            PicturePath = PICTURE__PATH_PREFIX.message + keyword + PICTURE_CONCAT.message + keyword + PICTURE_PATH_SUFFIX.message;
        } else if (!picturePath.contains("_f.jpg")) {
            String keyword = picturePath.replace(".jpg", "");
            PicturePath = PICTURE__PATH_PREFIX.message + keyword + PICTURE_CONCAT.message + keyword + PICTURE_PATH_SUFFIX.message;
        }
        return PicturePath;
    }


    /**
     * 上传图片方法
     */
    public static Integer fileUpload(MultipartFile multipartFile) {
        String originalFilename = multipartFile.getOriginalFilename();
        //String newFileName = UUID.randomUUID() + Objects.requireNonNull(originalFilename).substring(originalFilename.lastIndexOf("."));
        try {
            multipartFile.transferTo(new File(SYS_UPLOAD_FILE_PATH.getMessage() + originalFilename));
        } catch (IOException e) {
            e.printStackTrace();
            return 0;
        }
        return 1;
    }

    /**
     * 生成随机数
     */
    public static String getRandomNum(Integer length) {
        Random rd = new Random();
        StringBuilder num = new StringBuilder();
        for (int i = 1; i <= length; i++) {
            num.append(rd.nextInt(10));
        }
        return DateUtils.getNoConnectorDateFormat() + num;
    }

    /**
     * 处理前台传输的OrderInfo对象数据
     */
    public static OrderInfo handleOrderInfoData(OrderInfo orderInfo) {
        orderInfo.setCreateTime(DateUtils.getTodayLocalDateTime())
                .setImage("http://127.0.0.1:8081/uploadFiles/" + orderInfo.getImage())
                .setOrderNo(getRandomNum(7))
                .setTaskNum(orderInfo.getOrderDetailList().size())
                .setTaskNumNot(orderInfo.getOrderDetailList().size());
        if (orderInfo.getPaidMoney() == 0) {
            orderInfo.setBalanceFlag(0);
            orderInfo.setPaidFlag(2);
        } else {
            if (orderInfo.getPayment() == 0) {
                orderInfo.setBalanceFlag(2);
                orderInfo.setPaidFlag(1);
            } else {
                orderInfo.setBalanceFlag(1);
                orderInfo.setPaidFlag(2);
            }
        }
        return orderInfo;
    }

    /**
     * 处理前台传输的OrderDetail集合数据
     */
    public static List<TOrderDetail> handleOrderDetailData(OrderInfo orderInfo) {
        orderInfo.getOrderDetailList().forEach(item -> {
            item.setTaskNo(getRandomNum(4))
                    .setOrderId(orderInfo.getId())
                    .setCompanyId(orderInfo.getOrgId())
                    .setCompanyName(orderInfo.getOrgName())
                    .setMakeId(orderInfo.getSalesmanId())
                    .setMakeName(orderInfo.getSalesmanName())
                    .setPaidMoney(0)
                    .setPayment(item.getMoney())
                    .setCreateTime(DateUtils.getTodayDateObjDateTime());
        });
        return orderInfo.getOrderDetailList();
    }
}
